<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/15
 * Time: 20:20
 */
namespace App\Services;

use App\Handlers\ApiResponse;

class CommonService
{
    use ApiResponse;

    use RedisService;

    /**
     * 检查注册和忘记密码的短信验证码
     * @param $mobile
     * @param $code
     * @param $type
     * @return bool
     */
    public function checkCode($mobile, $code, $type)
    {
        $redisKey = "";
        if ('register' == $type) {
            $redisKey = $this->redisKeyCodeUserRegister() . $mobile;
        }
        if ('forget_password' == $type) {
            $redisKey = $this->redisKeyCodeUserForgetPassword() . $mobile;
        }
        if ('forget_pay_password' == $type) {
            $redisKey = $this->redisKeyCodeUserForgetPayPassword() . $mobile;
        }
        if ('truename_auth' == $type) {
            $redisKey = $this->redisKeyCodeUserTrueNameAuth() . $mobile;
        }
        if ('bind_bank_card' == $type) {
            $redisKey = $this->redisKeyCodeUserBindBankCard() . $mobile;
        }
        if ('bind_credit_card' == $type) {
            $redisKey = $this->redisKeyCodeUserBindCreditCard() . $mobile;
        }
        if ('credit_card_handle' == $type) {
            $redisKey = $this->redisKeyCodeCreditCardHandle() . $mobile;
        }
        $redisCode = $this->redis()->get($redisKey);
        if ($code != $redisCode) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 上传图片
     * @param $storageType
     * @param $folder
     * @param $file
     * @return bool|\Illuminate\Http\JsonResponse|string
     */
    public function upload($storageType, $folder, $file)
    {
        $uploadOSSorStorage = new UploadOSSorStorage();
        // 本地文件是否上传成功
        if (!$uploadOSSorStorage->isValidFile($file)) return $this->responseJson(10200);//无效文件
        //上传文件类型
        if (!$uploadOSSorStorage->isValidFileType($file)) return $this->responseJson(10201);//图片格式不支持
        //验证文件大小是否超出限制
        $fileLimitSize = config('filesystems.disks.image_size');
        if (!$uploadOSSorStorage->isValidFileSize($file, $fileLimitSize)) return $this->responseJson(10202);//图片大小超出限制
        $isUpload = $uploadOSSorStorage->uploadOSSorStorageLocal($file, $fileLimitSize, $storageType, $folder);
        if (!$isUpload) {
            return $this->responseJson(10203);//图片上传失败
        }
        return $isUpload;
    }

    /**
     * 还款计划手续费 四入五入，保留两位小数
     * ceil(7.832222222222 * 100) / 100
     * @param $repaymentMoney 还款金额
     * @param $personalRate 个人费率
     * @param $days 天数
     * @param $times 多少笔
     * @param $secondRate 秒到费
     * @return float
     */
    public function repaymentPlanServiceCharge($repaymentMoney, $personalRate, $days, $times = 3, $secondRate = 1)
    {
        return ceilDecimal(($repaymentMoney * $personalRate + $days * $times * $secondRate) / (1 - $personalRate), 2);
    }

    /**
     * 多出一笔 手续费算法
     * @param $personalRate
     * @param $secondRate
     * @return float|int
     */
    public function overOnetTimesRepaymentPlanServiceCharge($personalRate, $secondRate)
    {
        return ceilDecimal($secondRate / (1 - $personalRate), 2);
    }

    /**
     * 还款计划消费手续费 四舍五入，保留两位小数
     * ceil(7.832222222222 * 100) / 100
     * @param $repaymentMoney 还款金额
     * @param $personalRate 个人费率
     * @param $days 天数
     * @param $times 多少笔
     * @param $secondRate 秒到费
     * @return float
     */
    public function consumeServiceCharge($repaymentMoney, $personalRate, $secondRate)
    {
        return round($repaymentMoney * $personalRate + $secondRate, 2);
    }

    /**
     * 还款金提现金额
     * @param $repaymentEveryTimeMoney
     * @param $noConsumeMoney
     * @param $repaymentPlanServiceCharge
     * @param $alreadyConsumeServiceCharge
     * @return float
     */
    public function repaymentWithdrawMoney($repaymentEveryTimeMoney, $noConsumeMoney, $repaymentPlanServiceCharge, $alreadyConsumeServiceCharge)
    {
        return round($repaymentEveryTimeMoney - $noConsumeMoney + $repaymentPlanServiceCharge - $alreadyConsumeServiceCharge, 2);
    }

    /**
     * 等级收益
     * 还款金额 * （当级使用费率 - 当级结算费率）* 当级分润比例
     */
    public function profitLevel($money, $levelRate, $levelClearingRate, $profitPercent)
    {
        $rateDiff = $levelRate - $levelClearingRate;
        if ($rateDiff == 0) {
            return roundNo($money  * $profitPercent);
        }
        return roundNo($money * $rateDiff * $profitPercent);
    }

    /**
     * 办卡分享参数
     * @param $invitedUserId
     * @param $creditCardHandleId
     * @param $cardUrl
     * @param $title
     * @param $keywords
     * @return array
     */
    public function shareParams($invitedUserId, $creditCardHandleId, $cardUrl, $title, $keywords)
    {
        $params = 'invite_user_id=' . $invitedUserId . '&credit_card_handle_id=' . $creditCardHandleId;
        $encryptParams = encrypt($params);
        $shareUrl = config('app.url') . '/credit_card_handle/apply?param=' . $encryptParams;
        $data = [
            'share_thumb_url' => $cardUrl,
            'share_title' => $title,
            'share_descrition' => $keywords,
            'share_url' => $shareUrl,
        ];
        return $data;
    }
}